package com.wanxi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.ManagerService;
import com.wanxi.util.Tool;
import com.wanxi.vo.ManagerVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/manager")
@CrossOrigin(origins = "http://localhost:8081")
public class ManagerController {
    @Autowired
    ManagerService managerService;

    @GetMapping("/findAll")
    public String findAllManager(){
        ResultDTO allManager = managerService.findAllManager();
        return JSONObject.toJSONString(allManager);
//        return allManager;
    }

    @PutMapping("/add")
    public String addManager(ManagerVO managerVO){
        ResultDTO resultDTO = managerService.addManager(managerVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @PostMapping("/findById")
    public String findManagerById(ManagerVO managerVO){
        ResultDTO managerById = managerService.findManagerById(managerVO);
        return JSONObject.toJSONString(managerById);
    }

    @DeleteMapping("/delete")
    public String deleteManager(ManagerVO managerVO){
        ResultDTO resultDTO = managerService.deleteManager(managerVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @GetMapping("/findBySome")
    public String findManagerBySome(ManagerVO managerVO){
        ResultDTO managerBySome = managerService.findManagerBySome(managerVO);
        return JSONObject.toJSONString(managerBySome);
    }

    @PostMapping("/updateEnable")
    public String updateManagerEnable(ManagerVO managerVO){
        ResultDTO resultDTO = managerService.updateEnable(managerVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @PostMapping("/update")
    public String updateManagerById(ManagerVO managerVO){
        ResultDTO resultDTO = managerService.updateById(managerVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @PostMapping("/updatePassword")
    public String updateManagerPassword(ManagerVO managerVO,
                                        @Param("createTime") String createTime){
        managerVO.setCreateTime(Tool.strToDate(createTime));
        ResultDTO resultDTO = managerService.updatePassword(managerVO);
        return JSONObject.toJSONString(resultDTO);
    }
}
